Skip to content

[Windows][CV2][Net11] Fix CollectionView exception thrown when ItemsSource is set to Null - #39554

Merged
kubaflo merged 3 commits into
dotnet:net11.0from
devanathan-vaithiyanathan:fix-cv2-itemsource-null-scenario
Oct 26, 2026
Merged

kubaflo merged 3 commits into
dotnet:net11.0from
devanathan-vaithiyanathan:fix-cv2-itemsource-null-scenario

Conversation

@devanathan-vaithiyanathan

Copy link
Copy Markdown
Contributor

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Issue Details:

On Windows, changing a CollectionView’s ItemsSource from a populated collection to null, or restoring a populated collection afterward, can throw “The parameter is incorrect.”

Root cause:

The handler assigned CollectionViewSource.View even when its underlying Source was null.
Cleanup cleared the old collection source while WinUI still referenced it.
When no MAUI ItemTemplate was supplied, the handler also cleared WinUI’s automatically generated default template. This affected subsequent source restoration.

Description of Change

Updated ItemsViewHandler2.Windows.cs:

  • Assign native ItemsSource = null when the underlying source is null.
  • Defer clearing the old collection source until after the native control switches to the new source.
  • Remove the redundant native source reset from cleanup.
  • Preserve WinUI’s default item template; clear it only when it is a MAUI ItemFactory.

Issues Fixed

Fixes #39552

Tested the behavior in the following platforms.

  • Android
  • Windows
  • iOS
  • Mac
Before After
Windows
Before.mp4
Windows
After.mp4

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 39554

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 39554"

@dotnet-policy-service dotnet-policy-service Bot added the partner/syncfusion Issues / PR's with Syncfusion collaboration label Oct 22, 2026
@azure-pipelines

This comment has been minimized.

@devanathan-vaithiyanathan devanathan-vaithiyanathan added the community ✨ Community Contribution label Oct 22, 2026
@github-actions github-actions Bot added area-controls-collectionview CollectionView, CarouselView, IndicatorView platform/windows labels Oct 22, 2026
@devanathan-vaithiyanathan devanathan-vaithiyanathan changed the title [Windows][CV2][Net11] Fix CollectionView exception thrown when ItemsSource is set to Null [WIP][Windows][CV2][Net11] Fix CollectionView exception thrown when ItemsSource is set to Null Oct 22, 2026
@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Oct 22, 2026
@kubaflo

This comment has been minimized.

@azure-pipelines

This comment has been minimized.

@devanathan-vaithiyanathan devanathan-vaithiyanathan changed the title [WIP][Windows][CV2][Net11] Fix CollectionView exception thrown when ItemsSource is set to Null [Windows][CV2][Net11] Fix CollectionView exception thrown when ItemsSource is set to Null Oct 23, 2026
@github-actions github-actions Bot added s/agent-review-in-progress AI review is currently running for this PR and removed s/agent-review-in-progress AI review is currently running for this PR labels Oct 23, 2026
@MauiBot

MauiBot commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@devanathan-vaithiyanathan — existing-test verification for commit ed4bc858.

Result PASSED Platform Windows Commit ed4bc858


🧪 Gate analysis — click to expand

✅ Passed. The selected verification satisfied the gate's passing conditions. Unmatched, skipped, or inconclusive groups are not passing evidence.

📱 Device tests
Test group Without fix With fix
CollectionViewTests (CollectionView2ItemsSourceCanBeClearedAndRestored) ✅ Failed as expected (482s) ✅ Passed (210s)
Execution log excerpts

Without fix CollectionViewTests (CollectionView2ItemsSourceCanBeClearedAndRestored)

ss the warning by specifying `x:SuppressXamlTrimWarnings=True` within the closest element. If not, the property path might be trimmed and will not be AOT compatible. [D:\a\1\s\src\Controls\src\Core\Controls.Core.csproj::TargetFramework=net11.0-windows10.0.19041.0]
Platform\Windows\TabbedPage\TabbedPageStyle.xaml(19,21): XamlCompiler warning WMC1510: Ensure the property path is trimming and AOT compatible by making use of 'Compiled Bindings (x:bind)' if possible or by specifying the 'x:DataType' directive with the respective binding data context and marking the type declaration with the 'WinRT.GeneratedBindableCustomProperty' attribute or the 'Microsoft.UI.Xaml.Data.Bindable' attribute. If you are unable to do either but can ensure the data type is attributed correctly, then you can also suppress the warning by specifying `x:SuppressXamlTrimWarnings=True` within the closest element. If not, the property path might be trimmed and will not be AOT compatible. [D:\a\1\s\src\Controls\src\Core\Controls.Core.csproj::TargetFramework=net11.0-windows10.0.19041.0]
    0 Error(s)
Time Elapsed 00:05:29.37
  Passed: 1
  Failed: 1
  Skipped: 0
  Total: 2
  Tests completed with exit code: 1

With fix CollectionViewTests (CollectionView2ItemsSourceCanBeClearedAndRestored)

uppress the warning by specifying `x:SuppressXamlTrimWarnings=True` within the closest element. If not, the property path might be trimmed and will not be AOT compatible. [D:\a\1\s\src\Controls\src\Core\Controls.Core.csproj::TargetFramework=net11.0-windows10.0.19041.0]
Platform\Windows\TabbedPage\TabbedPageStyle.xaml(19,21): XamlCompiler warning WMC1510: Ensure the property path is trimming and AOT compatible by making use of 'Compiled Bindings (x:bind)' if possible or by specifying the 'x:DataType' directive with the respective binding data context and marking the type declaration with the 'WinRT.GeneratedBindableCustomProperty' attribute or the 'Microsoft.UI.Xaml.Data.Bindable' attribute. If you are unable to do either but can ensure the data type is attributed correctly, then you can also suppress the warning by specifying `x:SuppressXamlTrimWarnings=True` within the closest element. If not, the property path might be trimmed and will not be AOT compatible. [D:\a\1\s\src\Controls\src\Core\Controls.Core.csproj::TargetFramework=net11.0-windows10.0.19041.0]
    0 Error(s)
Time Elapsed 00:02:31.19
  Passed: 2
  Failed: 0
  Skipped: 0
  Total: 2
  Tests completed successfully

PR head: ed4bc858.

Without-fix baseline: e6d22838.

@MauiBot MauiBot added s/agent-gate-passed AI verified tests catch the bug (fail without fix, pass with fix) and removed s/agent-review-in-progress AI review is currently running for this PR labels Oct 23, 2026
@sheiksyedm
sheiksyedm marked this pull request as ready for review October 26, 2026 10:43
Copilot AI balanced review requested due to automatic review settings October 26, 2026 10:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The claimed cleanup deferral remains unimplemented, requiring human confirmation of WinUI lifecycle safety.

1 open issue
What changed in this PR

This PR addresses Windows CollectionView2 exceptions when clearing and restoring ItemsSource, targeting issue #39552.

Changes:

  • Assigns a null native source when the underlying source is null.
  • Preserves WinUI’s default item template and removes a redundant native source reset.
  • Adds device tests for repeated clear-and-restore cycles, with and without a MAUI item template.
File Description
src/​Controls/​tests/​DeviceTests/​Elements/​CollectionView/​CollectionViewTests.Windows.cs Tests source transitions, empty-view visibility, and default-template preservation.
src/​Controls/​src/​Core/​Handlers/​Items2/​ItemsViewHandler2.Windows.cs Adjusts null-source assignment, template preservation, and cleanup.

Review effort: Balanced


PlatformView.ItemsSource = _collectionViewSource?.View;
// A CollectionViewSource with no Source does not provide a usable WinUI view.
PlatformView.ItemsSource = _collectionViewSource?.Source is null ? null : _collectionViewSource.View;
@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Oct 26, 2026
Preserve the null-source clear/restore regression theory alongside the independent upstream header BindingContext regression test.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 26, 2026 11:17
@kubaflo

kubaflo commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Resolved and pushed the conflict with the current net11.0 tip (c95f7b024f9) in ordinary merge commit 301bf93bdc3, on the original fix-cv2-itemsource-null-scenario branch.

The only conflict was overlapping test insertions in CollectionViewTests.Windows.cs. Preserved both the PR's CV2 null-ItemsSource clear/restore theory and upstream's header/footer BindingContext regression test. The handler merged automatically, retaining the null-source/default-template fix alongside upstream changes.

Validation: all eight selected managed CollectionView/ItemsView unit tests passed with native platform targets disabled; git diff --check passed for the resolved file, and the latest target merges cleanly. The scoped formatting check was stopped after exceeding three minutes and is not claimed as passed. These managed tests do not compile or execute the Windows-specific code; native Windows compilation and regression execution remain for Windows CI. No device/UI automation or XCTest was run.

@kubaflo
kubaflo merged commit 0e0f868 into dotnet:net11.0 Oct 26, 2026
3 of 13 checks passed
@github-actions github-actions Bot added this to the .NET 11.0-rc2 milestone Oct 26, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Cleanup still clears the old collection source before the native source switches, leaving the documented cleanup-order correction unimplemented.

1 open issue

Review effort: Balanced

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Oct 26, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 26, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-controls-collectionview CollectionView, CarouselView, IndicatorView collectionview-winui-cv2 community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/windows s/agent-gate-passed AI verified tests catch the bug (fail without fix, pass with fix)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Net11][Windows]CollectionView exception thrown when ItemsSource is set to Null

5 participants